<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Robot Terminal</title>
    <style>
        .container {
            display: block;
            align-items: left;
            flex: 1;
            flex-grow: 1;
            width: auto;
            /* justify-content: flex-start; */
            white-space:nowrap;
            margin-right: 1px;
        }

        .controls {
            display: block;
            align-items: left;
            justify-content: flex-start;
            margin-top: 10px;
            width: auto;
            margin-right: 10px
        }

        .blue-text {
            color: rgb(103, 103, 250);
        }

        input {
            display: inline-block;
            flex: 1;
            /* width: auto; */
            min-width: 10px;
            padding: 10px;
            margin-right: 10px;
        }

        button {
            display: inline-block;
            padding: 10px;
            background-color: #007BFF;
            width: 150;
            color: white;
            border: none;
            cursor: pointer;
            margin-left: 5px;
            flex-grow: 0;
            font-size: 24px;
            font-weight: bold;
            border-radius: 10px;
            vertical-align:top;
        }

        label {
            display: inline-block;
            margin-left: 10px;
        }
        .fig-ansi {
            font-size: 2pt;
            font-family: "Courier New", Monospace;
            white-space: pre;
        }

        #auto-scroll {
            display: inline-block;
            margin-left: 10px;
            width: auto;
            flex: 0;
        }

        #clear-button {
            display: inline-block;
            padding: 3px;
            background-color: rgb(119, 81, 25);
            border: 1px solid rgb(93, 30, 30);
            width: auto;
            font-size: 16px;
            margin-right: 10px;
            margin-left: 1px;
            flex-grow: 0;
            border-radius: 10px;
        }
        #send-button {
            padding: 7px;
            background-color: rgb(20, 68, 107);
            border: 1px solid rgb(2, 11, 95);
            width: 15vw;
            font-size: 20px;
            margin-right: 2px;
            margin-left: auto;
            flex-grow: 1;
            border-radius: 10px;
        }

        #output {
            width: 96%;
            height: 68vh;
            /* min-height: 200px; */
            overflow-y: scroll;
            border: 1px solid black;
            margin-right: 10px;
            padding: 10px;
        }
        #inputTerminal {
            width: 78vw;
            /* height: 68vh; */
            /* min-height: 200px; */
            overflow-y: scroll;
            border: 1px solid black;
            margin-right: 1px;
            padding: 10px;
        }

        @media (max-width: 600px) {
            .container {
                flex-direction: column;
            }
        }
    </style>
</head>

<body>
    <pre id="taag_output_text" style="float:left;" class="fig-ansi">
██████╗  ██████╗ ██████╗  ██████╗ ████████╗    ████████╗███████╗██████╗ ███╗   ███╗██╗███╗   ██╗ █████╗ ██╗     
██╔══██╗██╔═══██╗██╔══██╗██╔═══██╗╚══██╔══╝    ╚══██╔══╝██╔════╝██╔══██╗████╗ ████║██║████╗  ██║██╔══██╗██║     
██████╔╝██║   ██║██████╔╝██║   ██║   ██║          ██║   █████╗  ██████╔╝██╔████╔██║██║██╔██╗ ██║███████║██║     
██╔══██╗██║   ██║██╔══██╗██║   ██║   ██║          ██║   ██╔══╝  ██╔══██╗██║╚██╔╝██║██║██║╚██╗██║██╔══██║██║     
██║  ██║╚██████╔╝██████╔╝╚██████╔╝   ██║          ██║   ███████╗██║  ██║██║ ╚═╝ ██║██║██║ ╚████║██║  ██║███████╗
╚═╝  ╚═╝ ╚═════╝ ╚═════╝  ╚═════╝    ╚═╝          ╚═╝   ╚══════╝╚═╝  ╚═╝╚═╝     ╚═╝╚═╝╚═╝  ╚═══╝╚═╝  ╚═╝╚══════╝
    </pre>
    <br>

    <p id="output"></p>
    <div class="container">
        <input type="text" id="inputTerminal" placeholder="请输入文本">
        <button id="send-button" onclick="sendText()">发送</button>
    </div>
    <div class="controls">
        <label for="auto-scroll">自动跳转行尾</label>
        <input type="checkbox" id="auto-scroll" checked onchange="toggleAutoScroll()" />
        <button onclick="clearOutput()" id="clear-button">清空文本框</button>
    </div>



    <script>
        function jumpToEnd() {
            document.getElementById('output').scrollTop = document.getElementById('output').scrollHeight;
        }
        function toggleAutoScroll() {
            if (document.getElementById('auto-scroll').checked) {
                jumpToEnd();
            }
        }
        function clearOutput() {
            const output = document.getElementById('output');
            output.innerHTML = '';
        }
        function sendText() {
            const inputTerminal = document.getElementById('inputTerminal');
            const output = document.getElementById('output');
            const text = inputTerminal.value;
            const now = new Date();
            const timestamp = now.toLocaleTimeString();
            var currentURL = window.location.href;

            if (text) {
                output.innerHTML += `<span class="blue-text">[${timestamp}]<- ${text}</span><br>`;
                inputTerminal.value = '';
                sendAjax(text);
                if (document.getElementById('auto-scroll').checked) { jumpToEnd(); }
            }
        }
        function sendAjax(text) {
            fetch('terminal', {
                method: 'POST',
                headers: {
                    'Content-Type': 'text/plain'
                },
                body: text
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok');
                    }
                    return response.text();
                })
                .then(data => {
                    console.log('Success:', data);

                })
                .catch((error) => {
                    console.error('Error:', error);
                });
        }
        document.addEventListener('keydown', function (event) {
            if (event.key === 'Enter') {
                sendText();
            }
        });
        function updateMSG() {
            const text = '?';
            const now = new Date();
            const timestamp = now.toLocaleTimeString();
            fetch('update', {
                method: 'POST',
                headers: {
                    'Content-Type': 'text/plain'
                },
                body: text
            })
                .then(response => {
                    if (response.status === 200) {
                        return response.text();
                    } else {
                        throw new Error('Network response was not ok:' + response.text());
                    }
                })
                .then(data => {
                    const formattedData = data.replace(/\n/g, '<br>');
                    output.innerHTML += `[${timestamp}]-> ${formattedData}<br>`;
                    if (document.getElementById('auto-scroll').checked) { jumpToEnd(); }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }
        setInterval(updateMSG, 1000);
    </script>
</body>

</html>